//
//  PerMothView.m
//  TestCalendar
//
//  Created by yull on 14/12/2.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "PerMonthView.h"
#import "DayCell.h"
#import "UIView+Frame.h"

@interface PerMonthView ()

@property (nonatomic, strong) NSMutableArray *dayArray;

@end

@implementation PerMonthView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.itemSize = CGSizeMake(self.width/7, self.width/7 + 13);
        
        self.dayArray = [NSMutableArray array];
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init] ;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.itemSize = self.itemSize;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.sectionInset = UIEdgeInsetsZero;
        
        self.dayCollectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        self.dayCollectionView.delegate = self;
        self.dayCollectionView.dataSource = self;
        self.dayCollectionView.backgroundColor = [UIColor clearColor];
        self.dayCollectionView.bounces = NO;
        self.dayCollectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        self.dayCollectionView.scrollEnabled = NO;
        [self addSubview:self.dayCollectionView];
        
        [self.dayCollectionView registerClass:[DayCell class] forCellWithReuseIdentifier:@"DayCell"];
    }
    return self;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dayArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    DayCell *dayCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DayCell" forIndexPath:indexPath];
    dayCell.titleLabel.text = [self.dayArray objectAtIndex:indexPath.row];
    dayCell.planArray = [self.planDict objectForKey:dayCell.titleLabel.text];
    [dayCell refreshCellImage];
    return dayCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *day = [self.dayArray objectAtIndex:indexPath.row];
    if (day.length > 0 && [self.delegate respondsToSelector:@selector(clickAtDay:)]) {
        [self.delegate clickAtDay:indexPath.row-self.firstDay+2];
    }
}

- (void)reloadData
{
    if (!self.dayArray) {
        self.dayArray = [NSMutableArray array];
    }else {
        [self.dayArray removeAllObjects];
    }
    
    for (NSInteger i = 1;i <self.firstDay;i++) {
        [self.dayArray addObject:@""];
    }
    
    for (NSInteger i = 0;i < self.dayCount;i++) {
        [self.dayArray addObject:[NSString stringWithFormat:@"%d",i+1]];
    }
    
    [self.dayCollectionView reloadData];
}

- (BOOL)insertPlanInfo:(PlanInfo *)planInfo atPoint:(CGPoint)point
{
    NSIndexPath *indexPath = [self.dayCollectionView indexPathForItemAtPoint:point];
    
    NSString *str = [self.dayArray objectAtIndex:indexPath.row];
    
    if (str.length > 0) {
        
        planInfo.day = [str integerValue];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyy-MM-dd";
        NSTimeZone *timeZone = [NSTimeZone localTimeZone];
        NSDate *selectDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%2d-%2d", planInfo.year, planInfo.month, planInfo.day]];
        selectDate = [NSDate dateWithTimeInterval:[timeZone secondsFromGMT] sinceDate:selectDate];
    
        NSTimeInterval nowTimeInterval = floor([[NSDate date] timeIntervalSince1970]);
        NSDate *todayDate = [NSDate dateWithTimeIntervalSince1970:nowTimeInterval - ((unsigned long long)nowTimeInterval % 86400)];
        
        if ([selectDate compare:todayDate] == NSOrderedAscending) {
            if ([self.delegate respondsToSelector:@selector(insertFailed:)]) {
                [self.delegate insertFailed:@"计划只能定在今天以后"];
            }
            
            return NO;
        }
        
        DayCell *dayCell = (DayCell *)[self.dayCollectionView cellForItemAtIndexPath:indexPath];
        
        if (dayCell.planArray.count < 3) {
            [dayCell.planArray addObject:planInfo];
            
            [dayCell refreshCellImage];
            
            return YES;
        } else {
            if ([self.delegate respondsToSelector:@selector(insertFailed:)]) {
                [self.delegate insertFailed:@"最多只能添加3个运动"];
            }
            return NO;
        }
    }
    
    return NO;
}

- (void)deletePlanInfo:(PlanInfo *)planInfo
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:planInfo.day-1 inSection:0];
    
    DayCell *dayCell = (DayCell *)[self.dayCollectionView cellForItemAtIndexPath:indexPath];
    [dayCell.planArray removeObject:planInfo];
    
    [dayCell refreshCellImage];
}

- (void)insertPlanInfo:(PlanInfo *)planInfo
{
    NSInteger pre = 0;
    for (NSString *str in self.dayArray) {
        if (str.length > 0) {
            break;
        }
        
        pre ++;
    }
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:planInfo.day-1+pre inSection:0];
    
    DayCell *dayCell = (DayCell *)[self.dayCollectionView cellForItemAtIndexPath:indexPath];
    [dayCell.planArray addObject:planInfo];
    
    [dayCell refreshCellImage];
}
@end
